<?php

/*
 * 回复信息
 */

/**
 * Description of Autoresponders
 *
 * @author Administrator
 */
class Autoresponders {
    private $CI;
    private $data;//数据保存空间

    public function __construct()
    {
        $this->CI =& get_instance();
        $this->CI->load->model('autoresponders_language_model');
    }
	
    /*
     * 获得回复语句 计算最相近的语句
     * @param string msg 传入一条语句 
     * return string
     */
    public function msg($msg)
    {

        $this->data = $this->CI->cache->memcached->get('autoresponders_language');
        if(!$this->data)
        {
            $memcache_t = $this->CI->autoresponders_language_model->lists();
            $t_data = new stdClass();
            foreach($memcache_t as $item)
            {
                $t_data->{$item->qualification} = $item;
            }
            $this->CI->cache->memcached->save('autoresponders_language', serialize($t_data), 3600*24*30);
            $this->data = $t_data;
        }
        else
        {
            $this->data = unserialize($this->data);
        }

        $msg = (string)$msg;
        $max_keyword = 0;
        $return = false;//返回信息
        $this->CI->load->model('unrecognized_model');
		foreach($this->data as $key=>$item)
		{
				similar_text($key,$msg,$percent);
				if($percent > $max_keyword)
				{
					$max_keyword = $percent;
					$return = $item->reply;
				}
		}
        if($max_keyword <= 50)
        {
            $this->CI->unrecognized_model->add($msg);
        }
		if($max_keyword < 30)
		{
			$return = false;
		}
    	return $return;
    }

}